﻿using System;
using System.Web.Mvc;
using System.Text;

namespace Milo.Eg.Web
{
    using Framework;

    public static class HtmlHelperExtensions
    {
        public static string RenderFieldControl(this HtmlHelper helper, IControl control)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div id='");
            sb.Append(control.ClientID);

            if (control.Visible)
                sb.Append("'/>");
            else
                sb.Append("' style='display: none;'/>");

            sb.Append("<label for='").Append(control.ClientID).Append("_input'/>").Append(control.Name).Append("</label>");
            sb.Append("<input id='").Append(control.ClientID).Append("_input' name='").Append(control.ModelID).
                Append("' value='").Append(helper.Encode(control.ToString())).Append("'/>");
            sb.Append("</div>");

            return sb.ToString();
        }

        public static string RenderTrigger(this HtmlHelper helper, ITriggerControl control)
        {
            ActionMethodInfo ami = control.GetActionMethodInfo();

			return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, control.Name, ami.ActionName, ami.ControllerName,
				new System.Web.Routing.RouteValueDictionary(ami.Parameters), null);
        }

		public static string RenderEachPartialView(this HtmlHelper helper, string partialViewName, System.Collections.IEnumerable list)
		{
			foreach (object model in list)
				System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(helper, partialViewName, model);

			return string.Empty;
		}
    }
}
